// import arcGisApiInstance from './arcGisApiInstance'
//地图通用操作
let arcGisMapUtils = {
    //图层
    layer: {
        /**
         * 创建天地图图层
         * 只支持经纬度坐标（需要平面的麦卡托坐标可以自行修改）
         * @param map 地图
         * @param tdtLayerType 天地图图层类型，支持以下值：img=影像——经纬度，cia=影像注记——经纬度，vec=矢量——经纬度，cva=矢量注记——经纬度,ter=地形图——经纬度，cta=地形图注记——经纬度
         * @param layerConfig 图层配置
         * @param token 发布文档有（或者问发布人）
         */
        createTdtLayer: function (arcGisApiInstance,map,tdtLayerType, layerConfig,token) {
            //切片配置,
            //PS：加载天地图图层本质上是用自定义切片图层
            let tileInfo = new arcGisApiInstance.TileInfo({
                "rows": 256,
                "cols": 256,
                "compressionQuality": 0,
                //切片原点
                "origin": {
                    "x": -180,
                    "y": 90
                },
                //坐标系
                "spatialReference": {
                    //本方法只支持经纬度坐标，坐标系为wgs84
                    "wkid": 4326
                },
                "lods": [
                    //天地图固定的切片级别
                    { "level": 0, "resolution": 1.40625, "scale": 590995186.11750006 },
                    { "level": 1, "resolution": 0.703125, "scale": 295497593.05875003 },
                    {"level": 2, "resolution": 0.3515625, "scale": 147748796.52937502},
                    {"level": 3, "resolution": 0.17578125, "scale": 73874398.264687508},
                    {"level": 4, "resolution": 0.087890625, "scale": 36937199.132343754},
                    {"level": 5, "resolution": 0.0439453125, "scale": 18468599.566171877},
                    {"level": 6, "resolution": 0.02197265625, "scale": 9234299.7830859385},
                    {"level": 7, "resolution": 0.010986328125, "scale": 4617149.8915429693},
                    {"level": 8, "resolution": 0.0054931640625, "scale": 2308574.9457714846},
                    {"level": 9, "resolution": 0.00274658203125, "scale": 1154287.4728857423},
                    {"level": 10, "resolution": 0.001373291015625, "scale": 577143.73644287116},
                    {"level": 11, "resolution": 0.0006866455078125, "scale": 288571.86822143558},
                    {"level": 12, "resolution": 0.00034332275390625, "scale": 144285.93411071779},
                    {"level": 13, "resolution": 0.000171661376953125, "scale": 72142.967055358895},
                    {"level": 14, "resolution": 8.58306884765625e-005, "scale": 36071.483527679447},
                    {"level": 15, "resolution": 4.291534423828125e-005, "scale": 18035.741763839724},
                    {"level": 16, "resolution": 2.1457672119140625e-005, "scale": 9017.8708819198619},
                    {"level": 17, "resolution": 1.0728836059570313e-005, "scale": 4508.9354409599309},
                    {"level": 18, "resolution": 5.3644180297851563e-006, "scale": 2254.4677204799655},
                    {"level": 19, "resolution": 2.68220901489257815e-006, "scale": 1127.23386023998275},
                    {"level": 20, "resolution": 1.341104507446289075e-006, "scale": 563.616930119991375},
                    {"level": 21, "resolution": 6.705522537231445375e-007, "scale": 281.8084650599956875},
                    {"level": 22, "resolution": 3.3527612686157226875e-007, "scale": 140.90423252999784375},
                    {"level": 23, "resolution": 1.67638063430786134375e-007, "scale": 70.452116264998921875},
                    {"level": 24, "resolution": 8.38190317153930671875e-008, "scale": 35.2260581324994609375}
                ]
            });
            //设置图层全图范围
            let spatialReference = new arcGisApiInstance.SpatialReference({wkid: 4326});
            let fullExtent = new arcGisApiInstance.Extent(-180.0, -90.0, 180.0, 90.0, spatialReference);

            //图层配置
            let _layerConfig = layerConfig || {};
            let tk = token||'';
            let _defaultLayerConfig = {
                //天地图切片的url模板
                urlTemplate: "http://{subDomain}.tianditu.com/" + tdtLayerType + "_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=" + tdtLayerType + "&STYLE=default&TILEMATRIXSET=c&TILEMATRIX={level}&TILEROW={row}&TILECOL={col}&FORMAT=tiles&tk="+ tk,
                subDomains: ["t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7"],
                copyright: "",
                spatialReference: spatialReference,
                fullExtent: fullExtent,
                tileInfo: tileInfo,
            };
            _layerConfig = Object.assign(_defaultLayerConfig,_layerConfig);
            //根据图层配置新建图层
            let layer = new arcGisApiInstance.WebTileLayer(_layerConfig);
            map.add(layer);
            return layer;
        },
        /**
         * 创建图形图层并添加到地图
         * @param arcGisApiInstance
         * @param map 地图
         * @param layerConfig 图层配置
         */
        createGraphicsLayerAndAddToMap: function (arcGisApiInstance,map,layerConfig) {
            let layer = new arcGisApiInstance.GraphicsLayer(layerConfig);
            //图层添加到地图
            map.add(layer);
            // map.addMany([layer]); // 批量添加
            return layer;
        },
        /**
         * 创建热力图并添加到地图
         * @param map 地图
         * @param layerConfig 图层配置
         */
        createHeatMapLayerAndAddToMap:function (arcGisApiInstance,map,layerConfig) {
            let heatmapRenderer = new arcGisApiInstance.HeatmapRenderer({//设置渲染器
                colorStops: [
                    {
                        color: "rgba(63, 40, 102, 0)",
                        ratio: 0
                    },
                    {
                        color: "#00f",
                        ratio: 0.183
                    },
                    {
                        color: "#0ff",
                        ratio: 0.266
                    },
                    {
                        color: "#0f0",
                        ratio: 0.549
                    },
                    {
                        color: "#ff0",
                        ratio: 0.732
                    },
                    {
                        color: "#f00",
                        ratio: 0.915
                    }],
                /*colorStops: [
                    { ratio: 0, color: "rgba(0, 255, 150, 0)" },
                    { ratio: 0.6, color: "rgb(250, 250, 0)" },
                    { ratio: 0.85, color: "rgb(250, 150, 0)"},
                    { ratio: 0.95, color: "rgb(255, 50, 0)"}],*/
                blurRadius: 6.5,
                /*maxPixelIntensity: 10000,
                minPixelIntensity: 0*/
            });
            let layerDefinition = {
                //source: source,//点数据集
                //objectIdField: "ObjectID",//重要！！！
                title: "热力图",
                renderer: heatmapRenderer//渲染器
            };
            layerDefinition = Object.assign({},layerDefinition,layerConfig);
            let heatMapLayer = new arcGisApiInstance.FeatureLayer(layerDefinition);
            map.add(heatMapLayer);
            return heatMapLayer;
        },

        creatTileLayer: function (arcGisApiInstance,map) {
            let url="https://www.nscsyy.com:6443/arcgis/rest/services/YXDT/lsdj_yxt/MapServer";
            // let layer  = new esri.layers.ArcGISTiledMapServiceLayer(url,{visible:true});  //  ArcGISDynamicMapServiceLayer
            let layer  = new arcGisApiInstance.VectorTileLayer({
                url:url
            });  //  ArcGISDynamicMapServiceLayer

            // let layer = new arcGisApiInstance.GraphicsLayer(layerConfig);
            //图层添加到地图
            map.add(layer);
            // map.addMany([layer]); // 批量添加
            return layer;
        }

    },
    draw:{
        view:null,
        layer:null,
        img:null,
        PolylineGraphic:null,
        PolygonGraphic:null,
        CircleGraphic:null,
        RectangleGraphic:null,
        imgSymbol:{
            type: 'picture-marker',
            url: '',
            width: '21px',
            height: '30px',
            xoffset: 0,//与坐标的x轴偏移
            yoffset: 10,//与坐标的y轴偏移
        },
        fillSymbol:{
            type:"simple-marker",
            color:[255,255,255],
            size:6,
            outline: {
                color: [ 255, 0, 0 ],
                width: 1.5  // points
            }
        },
        /**
         * 画点
         * @param view
         * @param layer
         * @param img  要显示的图元，若使用默认样式可传 null、''等判断值为 false的值
         * return graphic
         */
        DrawPoint(arcGisApiInstance,view,layer,img){
            arcGisMapUtils.draw.view = view;
            arcGisMapUtils.draw.layer = layer;
            arcGisMapUtils.draw.img = img?img:null;

            const draw = new arcGisApiInstance.Draw({
                view: view
            });
            arcGisMapUtils.draw.enableCreatePoint(arcGisApiInstance,draw, view);
        },
        /**
         * 画线
         * @param view
         * @param layer
         * return graphic
         */
        DrawLine(arcGisApiInstance,view,layer){
            arcGisMapUtils.draw.view = view;
            arcGisMapUtils.draw.layer = layer;

            const draw = new arcGisApiInstance.Draw({
                view: view
            });
            //清除之前绘制
           /* if(arcGisMapUtils.draw.PolylineGraphic){
                arcGisMapUtils.draw.layer.remove(arcGisMapUtils.draw.PolylineGraphic);
            }*/
            arcGisMapUtils.draw.enableCreateLine(arcGisApiInstance,draw, view);
        },
        /**
         * 画面
         * @param view
         * @param layer
         * return graphic
         */
        DrawArea(arcGisApiInstance,view,layer){
            arcGisMapUtils.draw.view = view;
            arcGisMapUtils.draw.layer = layer;

            const draw = new arcGisApiInstance.Draw({
                view: view
            });
            //arcGisMapUtils.draw.layer.removeAll();//清楚之前的绘制
            arcGisMapUtils.draw.enableCreateArea(arcGisApiInstance,draw, view);
        },
        /**
         * 画圆
         * @param view
         * @param layer
         * return graphic
         */
        DrawCircle(arcGisApiInstance,view,layer){
            arcGisMapUtils.draw.view = view;
            arcGisMapUtils.draw.layer = layer;

            const draw = new arcGisApiInstance.Draw({
                view: view
            });
            arcGisMapUtils.draw.enableCreateCircle(arcGisApiInstance,draw, view);
        },
        /**
         * 画矩形
         * @param view
         * @param layer
         * return graphic
         */
        DrawRectangle(arcGisApiInstance,view,layer){
            arcGisMapUtils.draw.view = view;
            arcGisMapUtils.draw.layer = layer;

            const draw = new arcGisApiInstance.Draw({
                view: view
            });
            arcGisMapUtils.draw.enableCreateRectangle(arcGisApiInstance,draw, view);
        },

        enableCreatePoint(arcGisApiInstance,draw, view){ //开始监听画点
            let action = draw.create("point", {
                mode: "click"//点击方式加点
            });
            // 获取焦点
            view.focus();
            action.on(
                [
                    "vertex-add",
                    "vertex-remove",
                    "draw-complete"
                ],
                arcGisMapUtils.draw.createPoint(arcGisApiInstance)
            );
        },
        enableCreateLine(arcGisApiInstance,draw, view){
            let action = draw.create("polyline", {
                mode: "click"//点击方式加点
            });
            // 获取焦点
            view.focus();
            action.on(
                [
                    "vertex-add",
                    "vertex-remove",
                    "cursor-update",
                    "redo",
                    "undo",
                    "draw-complete"
                ],
                arcGisMapUtils.draw.createPolyline(arcGisApiInstance)
            );
        },
        enableCreateArea(arcGisApiInstance,draw, view){
            let action = draw.create("polygon", {
                mode: "click"//点击方式加点
            });
            // 获取焦点
            view.focus();
            action.on(
                [
                    "vertex-add",
                    "vertex-remove",
                    "cursor-update",
                    "redo",
                    "undo",
                    "draw-complete"
                ],
                arcGisMapUtils.draw.createPolygon(arcGisApiInstance)
            );
        },
        enableCreateCircle(arcGisApiInstance,draw, view){
            let action = draw.create("circle", {
                mode: "click"//点击方式加点
            });
            // 获取焦点
            view.focus();
            action.on(
                [
                    "vertex-add",
                    "vertex-remove",
                    "cursor-update",
                    "redo",
                    "undo",
                    "draw-complete"
                ],
                arcGisMapUtils.draw.createCircle(arcGisApiInstance)
            );
        },
        enableCreateRectangle(arcGisApiInstance,draw, view){
            let action = draw.create("rectangle", {
                mode: "click"//点击方式加点
            });
            // 获取焦点
            view.focus();
            action.on(
                [
                    "vertex-add",
                    "vertex-remove",
                    "cursor-update",
                    "redo",
                    "undo",
                    "draw-complete"
                ],
                arcGisMapUtils.draw.createRectangle(arcGisApiInstance)
            );
        },
        createPoint(arcGisApiInstance,event){
            let symbol = '';
            if(arcGisMapUtils.draw.img){
                arcGisMapUtils.draw.imgSymbol.url =  `../../images/symbol/${arcGisMapUtils.draw.img}`;
                symbol = arcGisMapUtils.draw.imgSymbol;
            }else{
                symbol = arcGisMapUtils.draw.fillSymbol;
            }

            //获取所有顶点
            let coordinates = event.coordinates;
            //生成绘制的图形
            let graphic = new arcGisApiInstance.Graphic({
                geometry: new arcGisApiInstance.Point({
                    hasZ: false,
                    hasM: false,
                    x:coordinates[0],
                    y:coordinates[1],
                    spatialReference: arcGisMapUtils.draw.view.spatialReference
                }),
                symbol:symbol
            });
            // 将绘制的图形添加到layer
            arcGisMapUtils.draw.layer.add(graphic);
            return graphic;
        },
        createPolyline(arcGisApiInstance,event){
            //获取所有顶点
            let vertices = event.vertices;
            //清除之前绘制
            if(arcGisMapUtils.draw.PolylineGraphic){
                arcGisMapUtils.draw.layer.remove(arcGisMapUtils.draw.PolylineGraphic);
            }
            // 生成绘制的图形
            arcGisMapUtils.draw.PolylineGraphic = new arcGisApiInstance.Graphic({
                geometry: new arcGisApiInstance.Polyline({
                    paths: vertices,
                    spatialReference: arcGisMapUtils.draw.view.spatialReference
                }),
                symbol: {
                    type: "simple-line",
                    color: [255,116,3],
                    width: 4,
                    cap: "round",
                    join: "round"
                }
            });
            // 将绘制的图形添加到layer
            arcGisMapUtils.draw.layer.add(arcGisMapUtils.draw.PolylineGraphic);
            return arcGisMapUtils.draw.PolylineGraphic;
        },
        createPolygon(arcGisApiInstance,event){
            //获取所有顶点
            let vertices = event.vertices;
            //清除之前绘制
            if(arcGisMapUtils.draw.PolygonGraphic){
                arcGisMapUtils.draw.layer.remove(arcGisMapUtils.draw.PolygonGraphic);
            }
            // 生成绘制的图形
            arcGisMapUtils.draw.PolygonGraphic = new arcGisApiInstance.Graphic({
                geometry: new arcGisApiInstance.Polygon({
                    hasZ: false,
                    hasM: false,
                    rings: [vertices],
                    spatialReference: arcGisMapUtils.draw.view.spatialReference
                }),
                symbol: {
                    style: "solid",
                    type: "simple-fill",
                    color: [3, 255, 240, 0.1],
                    outline: {
                        color: [255,116,3],
                        width: 1
                    }
                }
            });
            // 将绘制的图形添加到layer
            arcGisMapUtils.draw.layer.add(arcGisMapUtils.draw.PolygonGraphic);
            return arcGisMapUtils.draw.PolygonGraphic;
        },
        createCircle(arcGisApiInstance,event){
            //获取所有顶点
            let vertices = event.vertices;
            //少于一个点无法展示圆
            if(vertices.length<2){
                return
            }
            //清除之前绘制
            if(arcGisMapUtils.draw.CircleGraphic){
                arcGisMapUtils.draw.layer.remove(arcGisMapUtils.draw.CircleGraphic);
            }
            //生成绘制的图形,两点画圆
            let center=new arcGisApiInstance.Point({
                hasZ: false,
                hasM: false,
                x:vertices[0][0],
                y:vertices[0][1],
                spatialReference: arcGisMapUtils.draw.view.spatialReference
            });
            let dis=center.distance(new arcGisApiInstance.Point({
                hasZ: false,
                hasM: false,
                x:vertices[1][0],
                y:vertices[1][1],
                spatialReference: arcGisMapUtils.draw.view.spatialReference
            }));
            arcGisMapUtils.draw.CircleGraphic = new arcGisApiInstance.Graphic({
                geometry: new arcGisApiInstance.Circle({
                    hasZ: false,
                    hasM: false,
                    center:center,
                    radius:dis,
                    spatialReference: arcGisMapUtils.draw.view.spatialReference
                }),
                symbol: {
                    style: "solid",
                    type: "simple-fill",
                    color: [3, 255, 240, 0.1],
                    outline: {
                        color: [255,116,3],
                        width: 1
                    }
                }
            });
            // 将绘制的图形添加到view
            arcGisMapUtils.draw.layer.add(arcGisMapUtils.draw.CircleGraphic);
            return arcGisMapUtils.draw.CircleGraphic;
        },
        createRectangle(arcGisApiInstance,event){
            //获取所有顶点
            let vertices = event.vertices;
            //两点画矩形
            if(vertices.length<2){
                return
            }
            let rings=[vertices[0],[vertices[0][0],vertices[1][1]],vertices[1],[vertices[1][0],vertices[0][1]]];
            //清除之前绘制
            if(arcGisMapUtils.draw.RectangleGraphic){
                arcGisMapUtils.draw.layer.remove(arcGisMapUtils.draw.RectangleGraphic);
            }
            // 生成绘制的图形
            arcGisMapUtils.draw.RectangleGraphic = new arcGisApiInstance.Graphic({
                geometry: new arcGisApiInstance.Polygon({
                    hasZ: false,
                    hasM: false,
                    rings: [rings],
                    spatialReference: arcGisMapUtils.draw.view.spatialReference
                }),
                symbol: {
                    style: "solid",
                    type: "simple-fill",
                    color: [3, 255, 240, 0.1],
                    outline: {
                        color: [255,116,3],
                        width: 1
                    }
                }
            });
            // 将绘制的图形添加到layer
            arcGisMapUtils.draw.layer.add(arcGisMapUtils.draw.RectangleGraphic);
            return arcGisMapUtils.draw.RectangleGraphic;
        }
    },
    //测量
    measure:{
        view:null,
        layer:null,
        /**
         * 测距
         * @param view
         * @param layer
         */
        MeasurementLength(arcGisApiInstance,view,layer){
            arcGisMapUtils.measure.view = view;
            arcGisMapUtils.measure.layer = layer;

            const draw = new arcGisApiInstance.Draw({
                view: view
            });
            const action = draw.create("polyline");
            view.focus();
            action.on(
                [
                    "vertex-add",
                    "vertex-remove",
                    "cursor-update",
                    "redo",
                    "undo",
                    "draw-complete"
                ],
                arcGisMapUtils.measure.createPolyline(arcGisApiInstance)
            );
        },
        /**
         * 测面
         * @param view
         * @param layer
         */
        MeasurementArea(arcGisApiInstance,view,layer){
            arcGisMapUtils.measure.view = view;
            arcGisMapUtils.measure.layer = layer;

            const draw = new arcGisApiInstance.Draw({
                view: view
            });
            const action = draw.create("polygon");
            view.focus();
            arcGisMapUtils.measure.polygonHandle = action.on(
                [
                    "vertex-add",
                    "vertex-remove",
                    "cursor-update",
                    "redo",
                    "undo",
                    "draw-complete"

                ],
                arcGisMapUtils.measure.createPolygon(arcGisApiInstance)
            );
        },
        createPolyline(arcGisApiInstance,event){
            let view = arcGisMapUtils.measure.view;
            let measurementGraphicsLayer = arcGisMapUtils.measure.layer;

            let vertices = event.vertices;
            let symbol={
                type:"simple-marker",
                color:[255,255,255],
                size:6,
                outline: {
                    color: [ 255, 0, 0 ],
                    width: 1.5  // points
                }
            };
            // view.graphics.removeAll();
            measurementGraphicsLayer.removeAll();
            let graphics=new arcGisApiInstance.Graphic({
                geometry:new arcGisApiInstance.Polyline({
                    paths: vertices,
                    spatialReference: view.spatialReference
                }),
                symbol: {
                    type: "simple-line",
                    color: [255,116,3],
                    width: 2,
                    cap: "round",
                    join: "round"
                }
            });
            measurementGraphicsLayer.add(graphics);
            let firsttextSymbol = {
                type: "text",
                color: "white",
                haloColor: "black",
                haloSize: "1px",
                text: "起点",
                xoffset: '10px',
                yoffset: '10px',
                font: {
                    size: 12,
                    family: "sans-serif",
                    weight: "bold"
                }
            };
            let firstpoint={
                type:"point",
                // longitude:vertices[0][0],
                // latitude:vertices[0][1],
                y:vertices[0][1],
                x:vertices[0][0],
                spatialReference: view.spatialReference
            };
            let firstTextGraphics=new arcGisApiInstance.Graphic({
                geometry:firstpoint,
                symbol: firsttextSymbol
            });
            let firstGraphics=new arcGisApiInstance.Graphic({
                geometry:firstpoint,
                symbol: symbol
            });
            measurementGraphicsLayer.add(firstTextGraphics);
            measurementGraphicsLayer.add(firstGraphics);
            let path=[];
            let arr1=[];
            arr1.push(vertices[0][0]);
            arr1.push(vertices[0][1]);
            path.push(arr1);
            let graphicMany = [];
            for (let i = 1; i <vertices.length ; i++) {
                let point={
                    type:"point",
                    // longitude:vertices[i][0],
                    // latitude:vertices[i][1],
                    x:vertices[i][0],
                    y:vertices[i][1],
                    spatialReference: view.spatialReference
                };
                let arr=[];
                arr.push(vertices[i][0]);
                arr.push(vertices[i][1]);
                path.push(arr);
                let line = new arcGisApiInstance.Polyline({
                    hasZ: false,
                    hasM: true,
                    paths: path,
                    spatialReference: view.spatialReference
                });
                let dislen;
                let unit;
                if(view.scale>5000) {
                    if(arcGisMapUtils.measure.isProject(view)){
                        dislen = arcGisApiInstance.geometryEngine.planarLength(line, 'kilometers');
                    }else{
                        dislen = arcGisApiInstance.geometryEngine.geodesicLength(line, 'kilometers');
                    }
                    unit="千米";
                }else {
                    if(arcGisMapUtils.measure.isProject(view)){
                        dislen = arcGisApiInstance.geometryEngine.planarLength(line, 'meters');
                    }else{
                        dislen = arcGisApiInstance.geometryEngine.geodesicLength(line, 'meters');
                    }
                    unit="米";
                }
                let textSymbol = {
                    type: "text",
                    color: "white",
                    haloColor: "black",
                    haloSize: "1px",
                    text: Math.abs(Math.round(dislen*100)/100)+unit,
                    xoffset: '20px',
                    yoffset: '20px',
                    font: {
                        size: 12,
                        family: "sans-serif",
                        weight: "bold"
                    }
                };
                let textGraphics=new arcGisApiInstance.Graphic({
                    geometry:point,
                    symbol: textSymbol
                });
                let Graphics=new arcGisApiInstance.Graphic({
                    geometry:point,
                    symbol: symbol
                });
                graphicMany.push(textGraphics);
                graphicMany.push(Graphics);
            }
            measurementGraphicsLayer.addMany(graphicMany);
            //console.log(textSymbol.text);
        },
        createPolygon(arcGisApiInstance,event){
            let view = arcGisMapUtils.measure.view;
            let measurementGraphicsLayer = arcGisMapUtils.measure.layer;

            let vertices = event.vertices;
            let symbol={
                type:"simple-marker",
                color:[255,255,255],
                size:6,
                outline: {
                    color: [ 255, 0, 0 ],
                    width: 1.5  // points
                }
            };
            let fillSymbol = {
                type: "simple-fill",
                color: [3, 255, 240, 0.1],
                outline: {
                    color: [255,116,3],
                    width: 2
                }
            };
            let polygon=new arcGisApiInstance.Polygon({
                rings: vertices,
                spatialReference: view.spatialReference
            });
            measurementGraphicsLayer.removeAll();
            let graphics=new arcGisApiInstance.Graphic({
                geometry:polygon,
                symbol: fillSymbol
            });
            measurementGraphicsLayer.add(graphics);
            let center = polygon.centroid;
            let area=0;
            let unit;
            if(view.scale>5000) {
                area = arcGisApiInstance.geometryEngine.geodesicArea(graphics.geometry,'square-kilometers');
                unit="平方千米";
            }else {
                area = arcGisApiInstance.geometryEngine.geodesicArea(graphics.geometry,'square-meters');
                unit="平方米";
            }
            let pointGraphicsMany = [];
            for (let i = 0; i <vertices.length ; i++) {
                let point={
                    type:"point",
                    // longitude:vertices[i][0],
                    //latitude:vertices[i][1],
                    x:vertices[i][0],
                    y:vertices[i][1],
                    spatialReference: view.spatialReference
                };
                let pointGraphics=new arcGisApiInstance.Graphic({
                    geometry:point,
                    symbol: symbol
                });
                pointGraphicsMany.push(pointGraphics);
            }
            measurementGraphicsLayer.addMany(pointGraphicsMany);
            let pointcenter={
                type:"point",
                /*longitude:center.longitude,
                latitude:center.latitude,*/
                x:center.longitude,
                y:center.latitude,
                spatialReference: view.spatialReference
            };
            let textSymbol = {
                type: "text",
                color: "white",
                haloColor: "black",
                haloSize: "5px",
                text:Math.abs(Math.round(area*100)/100)+unit,
                font: {
                    size: 12,
                    family: "sans-serif",
                    weight: "bold"
                }
            };
            let textGraphics=new arcGisApiInstance.Graphic({
                geometry:pointcenter,
                symbol: textSymbol
            });
            measurementGraphicsLayer.add(textGraphics);
            //console.log(textSymbol.text);
        },
        isProject(view) {
            return Math.abs(view.extent.ymax) > 90.0;
        },
    },
    //几何
    geometry: {
        /**
         * wkt转点几何对象
         * @param arcGisApiInstance
         * @param wkt wkt
         * @param sr 空间参考
         * @returns {arcGisApiInstance.Polyline}
         */
        wktToPoint: function ( arcGisApiInstance,wkt, sr) {
            //wkt转坐标对象
            let points = arcGisMapUtils.geometry.wktToCoords(wkt);
            //生成线
            let point = new arcGisApiInstance.Point({
                //坐标
                x: points[0],
                y: points[1],
                //空间参考
                spatialReference: sr
            });
            return point;
        },
        /**
         * wkt转点的图形（Graphic）
         * @param arcGisApiInstance
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值（可空）
         */
        wktToPointGraphic: function (arcGisApiInstance,wkt, style, sr, attributes) {
            //wkt转点的几何对象
            let point = arcGisMapUtils.geometry.wktToPoint( arcGisApiInstance,wkt, sr);

            //生成点样式
            let markerSymbol = new arcGisApiInstance.SimpleMarkerSymbol(
                style,
            );
            //生成图形
            let pointGraphic = new arcGisApiInstance.Graphic({
                //几何对象
                geometry: point,
                //样式
                symbol: markerSymbol,
                //属性字段
                attributes: attributes,
            });
            return pointGraphic;
        },
        /**
         * wkt转点的文字的图形（Graphic）
         * @param arcGisApiInstance
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值（可空）
         */
        wktToTextGraphic: function (arcGisApiInstance, wkt, style, sr, attributes) {
            //wkt转点的几何对象
            let point = arcGisMapUtils.geometry.wktToPoint( arcGisApiInstance,wkt, sr);

            //生成点样式，TextSymbol=文字样式
            let markerSymbol = new arcGisApiInstance.TextSymbol(
                style,
            );

            //生成图形
            let textGraphic = new arcGisApiInstance.Graphic({
                //几何对象
                geometry: point,
                //样式
                symbol: markerSymbol,
                //属性字段
                attributes: attributes,
            });

            return textGraphic;
        },
        /**
         * wkt转点的图片的图形（Graphic）
         * @param arcGisApiInstance
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值（可空）
         */
        wktToPicGraphic: function ( arcGisApiInstance, wkt, style, sr, attributes) {
            //wkt转点的几何对象
            let point = arcGisMapUtils.geometry.wktToPoint(arcGisApiInstance,wkt,sr);

            //生成点样式，PictureMarkerSymbol=图片点样式
            let pictureMarkerSymbol = new arcGisApiInstance.PictureMarkerSymbol(
                style,
            );

            //生成图形
            let picGraphic = new arcGisApiInstance.Graphic({
                //几何对象
                geometry: point,
                //样式
                symbol: pictureMarkerSymbol,
                //属性字段
                attributes: attributes,
            });

            return picGraphic;
        },
        /**
         * 坐标集合转点几何对象
         * @param arcGisApiInstance
         * @param {Array} coord   [113.33456,23.44678]/[54333333.111，234444444.555]
         * @param sr 空间参考
         */
        coordToPoint: function ( arcGisApiInstance,coord, sr) {
            //生成线
            let point = new arcGisApiInstance.Point({
                //坐标
                x: coord[0],
                y: coord[1],
                //空间参考
                spatialReference: sr
            });
            return point;
        },
        /**
         * 根据x y新建点几何对象
         * @param arcGisApiInstance
         * @param x   113.45\45566777.455
         * @param y    23.34\344556677.778
         * @param sr 空间参考
         */
        xyToPoint: function (arcGisApiInstance,x,y,sr) {
            //生成线
            let point = new arcGisApiInstance.Point({
                //坐标
                x: x,
                y: y,
                //空间参考
                spatialReference: sr
            });
            return point;
        },
        /**
         * 点转wkt
         * @param geometry
         * @returns {string}
         */
        pointToWkt: function (geometry) {
            return "POINT (" + geometry.x + " " + geometry.y + ")";
        },
        /**
         * 线几何对象转线的图形（Graphic）
         * @param arcGisApiInstance
         * @param geometry
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值（可空）
         */
        polylineToPolylineGraphic: function (arcGisApiInstance,geometry, style, sr, attributes) {
            //生成线样式
            let lineSymbol = new arcGisApiInstance.SimpleLineSymbol(
                style,
            );
            //生成图形
            let graphic = new arcGisApiInstance.Graphic({
                //几何对象
                geometry: geometry,
                //样式
                symbol: lineSymbol,
                //属性字段
                attributes: attributes,
            });
            return graphic;
        },
        /**
         * wkt转线的图形（Graphic）
         * @param arcGisApiInstance
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值（可空）
         */
        wktToPolylineGraphic: function (arcGisApiInstance, wkt, style, sr, attributes) {
            //wkt转线的几何对象
            let polyline = arcGisMapUtils.geometry.wktToPolyline( arcGisApiInstance,wkt, sr);

            //生成线样式
            let lineSymbol = new arcGisApiInstance.SimpleLineSymbol(
                style,
            );

            //生成图形
            let graphic = new arcGisApiInstance.Graphic({
                //几何对象
                geometry: polyline,
                //样式
                symbol: lineSymbol,
                //属性字段
                attributes: attributes,
            });

            return graphic;
        },
        /**
         * wkt转线几何对象
         * @param arcGisApiInstance api
         * @param wkt wkt
         * @param sr 空间参考
         */
        wktToPolyline: function (arcGisApiInstance,wkt, sr) {
            //wkt转坐标对象
            let points = arcGisMapUtils.geometry.wktToCoords(wkt);

            //生成线
            let polyline = new arcGisApiInstance.Polyline({
                //坐标
                paths: points,
                //空间参考
                spatialReference: sr
            });

            return polyline;
        },
        /**
         * 坐标转线几何对象
         * @param arcGisApiInstance
         * @param coord 坐标对象，格式如：[[[113.545949, 22.24015749], [113.56989, 22.24916], [113.55324, 22.220588]]]
         * @param sr 空间参考
         */
        coordToPolyline: function (arcGisApiInstance,coord, sr) {
            //生成线
            let polyline = new arcGisApiInstance.Polyline({
                //坐标
                paths: coord,
                //空间参考
                spatialReference: sr
            });

            return polyline;
        },
        /**
         * 线转wkt
         * @param geometry
         * @returns {string}
         */
        polylineToWkt: function (geometry) {
            let wkt = [];
            let paths = geometry.paths;
            for (let i in paths) {
                let path = paths[i];
                for (let j in path) {
                    let p = path[j];
                    wkt.push(p.join(" "));
                }
            }
            // return "LINESTRING (" + wkt.join(",") + ")";
            return "POLYLINE (" + wkt.join(",") + ")";
        },
        /**
         * wkt转面的图形（Graphic）
         * @param arcGisApiInstance
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值（可空）
         */
        wktToPolygonGraphic: function (arcGisApiInstance, wkt, style, sr, attributes) {
            //wkt转面的几何对象
            let polygon = arcGisMapUtils.geometry.wktToPolygon( arcGisApiInstance, wkt, sr);

            //生成面样式
            let fillSymbol = new arcGisApiInstance.SimpleFillSymbol(
                style,
            );

            //生成图形
            let graphic = new arcGisApiInstance.Graphic({
                //几何对象
                geometry: polygon,
                //样式
                symbol: fillSymbol,
                //属性字段
                attributes: attributes,
            });

            return graphic;
        },
        /**
         * 面几何对象转面的图形（Graphic）
         * @param arcGisApiInstance
         * @param wkt wkt
         * @param style 样式
         * @param sr 空间参考
         * @param attributes 属性字段值（可空）
         */
        polygonToPolygonGraphic: function ( arcGisApiInstance, geometry, style, sr, attributes) {
            //生成面样式
            let fillSymbol = new arcGisApiInstance.SimpleFillSymbol(
                style,
            );

            //生成图形
            let graphic = new arcGisApiInstance.Graphic({
                //几何对象
                geometry: geometry,
                //样式
                symbol: fillSymbol,
                //属性字段
                attributes: attributes,
            });

            return graphic;
        },
        /**
         * wkt转面几何对象
         * @param arcGisApiInstance
         * @param wkt wkt
         * @param sr 空间参考
         */
        wktToPolygon: function (arcGisApiInstance,  wkt, sr) {
            //wkt转坐标对象
            let points = arcGisMapUtils.geometry.wktToCoords(wkt);

            //生成面
            let polygon = new arcGisApiInstance.Polygon({
                //坐标
                rings: points,
                //空间参考
                spatialReference: sr
            });

            return polygon;
        },
        /**
         * 坐标对象转面几何对象
         * @param arcGisApiInstance
         * @param coord 坐标对象 格式是arcgis jsapi标准，例如：[[[113.527839, 22.27028], [113.527238, 22.2557786], [113.5437178, 22.2597268], [113.54423, 22.2730306], [113.527839, 22.27028]]]
         * @param sr 空间参考
         */
        coordToPolygon: function ( arcGisApiInstance, coord, sr) {
            //生成面
            let polygon = new arcGisApiInstance.Polygon({
                //坐标
                rings: coord,
                //空间参考
                spatialReference: sr
            });

            return polygon;
        },
        /**
         * 面转wkt
         * @param geometry
         * @returns {string}
         */
        polygonToWkt: function (geometry) {
            let wkt = [];
            let rings = geometry.rings;
            for (let i in rings) {
                let ring = rings[i];
                for (let j in ring) {
                    let p = ring[j];
                    wkt.push(p.join(" "));
                }
            }
            return "POLYGON ((" + wkt.join(",") + "))";
        },
        /**
         * wkt转坐标对象
         * PS：坐标对象是arcgis jsapi格式
         *  POINT[[[113.506211898131,22.7682394554548]]]
         * POLYLINE [[[113.5069549034056,22.775973814236735],[113.5060375879225,22.776311772572612],[113.50557088355391,22.776488798367595]]]
         * POLYGON [[[113.5069549034056,22.775973814236735],[113.5060375879225,22.776311772572612],[113.50557088355391,22.776488798367595]]]
         * @param wkt
         * @returns
         * [113.506211898131,22.7682394554548]
         * [[113.5069549034056,22.775973814236735],[113.5060375879225,22.776311772572612],[113.50557088355391,22.776488798367595]]
         * [[113.5069549034056,22.775973814236735],[113.5060375879225,22.776311772572612],[113.50557088355391,22.776488798367595]]
         */
        wktToCoords: function (wkt) {
            let coords, type, str;
            wkt = wkt.replace(/[\n\r]/g, " ");
            let matches = arcGisMapUtils.geometry.regExes.typeStr.exec(wkt);
            if (matches) {
                type = matches[1].toLowerCase();
                str = matches[2];
                if (arcGisMapUtils.geometry.parse[type]) {
                    coords = arcGisMapUtils.geometry.parse[type].apply(this, [str]);
                }
            }
            return coords;
        },
        /**
         *  wkt转坐标对象的正则表单式
         */
        regExes: {
            'typeStr': /^\s*(\w+)\s*\(\s*(.*)\s*\)\s*$/,
            'spaces': /\s+/,
            'parenComma': /\)\s*,\s*\(/,
            'doubleParenComma': /\)\s*\)\s*,\s*\(\s*\(/,  // can't use {2} here
            'trimParens': /^\s*\(?(.*?)\)?\s*$/
        },
        /**
         * wkt转坐标对象
         */
        parse: {
            'point': function (str) {
                let coords = arcGisMapUtils.util.trim(str).split(arcGisMapUtils.geometry.regExes.spaces);
                for (let i in coords)
                    coords[i] = Number(coords[i]);
                return coords;//new esri.geometry.Point(coords[0], coords[1]);
            },
            'pointzm': function (str) {
                let coords = arcGisMapUtils.util.trim(str).split(arcGisMapUtils.geometry.regExes.spaces);
                for (let i in coords)
                    coords[i] = Number(coords[i]);
                return coords.slice(0, 2);//new esri.geometry.Point(coords[0], coords[1]);
            },
            'linestring': function (str) {
                let points = arcGisMapUtils.util.trim(str).split(',');

                let components = [];
                for (let i = 0, len = points.length; i < len; ++i) {
                    components.push(arcGisMapUtils.geometry.parse.point.apply(this, [points[i]]));
                }
                return components//new esri.geometry.Polyline(components);
            },
            'linestringzm': function (str) {
                let points = arcGisMapUtils.util.trim(str).split(',');

                let components = [];
                for (let i = 0, len = points.length; i < len; ++i) {
                    components.push(arcGisMapUtils.geometry.parse.pointzm.apply(this, [points[i]]));
                }
                return components//new esri.geometry.Polyline(components);
            },
            'multilinestring': function (str) {
                let line;
                let lines = arcGisMapUtils.util.trim(str).split(arcGisMapUtils.geometry.regExes.parenComma);
                let components = [];
                for (let i = 0, len = lines.length; i < len; ++i) {
                    line = lines[i].replace(arcGisMapUtils.geometry.regExes.trimParens, '$1');
                    components.push(arcGisMapUtils.geometry.parse.linestring.apply(this, [line]));
                }
                return components;
            },
            'polygon': function (str) {
                let ring, linestring;
                let rings = arcGisMapUtils.util.trim(str).split(arcGisMapUtils.geometry.regExes.parenComma);

                let components = [];
                for (let i = 0, len = rings.length; i < len; ++i) {
                    ring = rings[i].replace(arcGisMapUtils.geometry.regExes.trimParens, '$1');
                    linestring = arcGisMapUtils.geometry.parse.linestring.apply(this, [ring]);
                    components.push(linestring);
                }
                return components;
            }
        },
    },
    //通用
    util: {
        trim: function (str) {
            return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        }
    },


};

export default arcGisMapUtils;

//wktToCoords  重要

//wktToPoint---------------pointToWkt
//coordToPoint
//xyToPoint

//wktToPointGraphic
//wktToTextGraphic
//wktToPicGraphic

//wktToPolyline---------------polylineToWkt
//coordToPolyline

//polylineToPolylineGraphic
//wktToPolylineGraphic

//wktToPolygon---------------polygonToWkt
//coordToPolygon

//polygonToPolygonGraphic
//wktToPolygonGraphic